<?php
/**
 * Num 记录和时间相关的类库中的方法
 */
namespace app\common\lib;

class Time
{
    /**
     * 生成登录token时间
     * @param int $type
     * @return float|int
     */
    public static function userLoginExpiresTime()
    {
        return 600;
    }

    /**
     * 时间
     * @param $date // 时间戳
     * @return false|string
     */
    public static function week($date){
        if(empty($date)){
            return false;
        }
        $date = date('Y-m-d',$date);
        $date = str_replace('/','-',$date);
        $dateArr = explode("-", $date);
        return date("N", mktime(0,0,0,$dateArr[1],$dateArr[2],$dateArr[0]));
    }

    /**
     * 计算两个时间戳相差的时间
     * @param string $begin_time // 开始时间戳
     * @param string $end_time // 结束时间戳
     * @return array
     */
    public static function timeDifference(string $begin_time ='', string $end_time =''){
        if(empty($begin_time) || empty($end_time) ){
            return false;
        }
        if($begin_time < $end_time){
            $starttime = $begin_time;
            $endtime = $end_time;
        }else{
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        //计算天数
        $timediff = $endtime-$starttime;
        $days = intval($timediff/86400);
        //计算小时数
        $remain = $timediff%86400;
        $hours = intval($remain/3600);
        //计算分钟数
        $remain = $remain%3600;
        $mins = intval($remain/60);
        //计算秒数
        $secs = $remain%60;
        return array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs);
    }
}
